/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_rtp_detect.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/17 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/rtp/config.h"
#include "../test_rtp_comm.h"
#include <rtp/rtp_core.h>
#include <rtp/rtp_header.h>
#include <rtp/rtp_comm.h>
#include <frct_comm.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "8060007700008ca00000099d000001ba44000c650401028f63f8";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 *   包长不符合要求
 */
static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "8060007700008ca00000";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 边界测试
 *   包长刚好小于要求
 */
static void __case3(void)
{

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "8060007700008ca0000009";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * Version标志错误
 */
static void __case4(void)
{

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "0060007700008ca00000099d000001ba44000c650401028f63f8";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * 非法的RTP payload type
 */
static void __case5(void)
{

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "8023007700008ca00000099d000001ba44000c650401028f63f8";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * ssrc为0
 */
static void __case6(void)
{

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "8060007700008ca000000000000001ba44000c650401028f63f8";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * CSRC个数超过包长
 */
static void __case7(void)
{

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtp_create_ct(&p_st_conn, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "8860007700008ca00000099d000001ba44000c650401028f63f8";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_RTP_SRC_IP, TEST_RTP_DST_IP, TEST_RTP_SRC_PORT, TEST_RTP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_rtp_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" rtp_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
